﻿using SketchUpAPI.NET.Objects;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SketchUpAPI.NET.Geometry
{
    /// <summary>
    /// 长方体
    /// </summary>
    public class Box : IGeometry
    {
        /// <summary>
        /// 长（x 方向）
        /// </summary>
        public double Length { get; set; }

        /// <summary>
        /// 宽（y 方向）
        /// </summary>
        public double Width { get; set; }

        /// <summary>
        /// 高（z 方向）
        /// </summary>
        public double Height { get; set; }

        /// <summary>
        /// 基础点
        /// </summary>
        /// <remarks>长方体底面的左下角点</remarks>
        public Point3D BasePoint { get; set; }

        private Vector3D _dirX = Vector3D.UnitX;
        /// <summary>
        /// X方向
        /// </summary>
        public Vector3D DirectorX
        {
            get
            {
                return _dirX;
            }
            set
            {
                _dirX = value.Unit;
            }
        }

        private Vector3D _dirY = Vector3D.UnitY;
        /// <summary>
        /// Y方向
        /// </summary>
        public Vector3D DirectorY
        {
            get
            {
                return _dirY;
            }
            set
            {
                _dirY = value.Unit;
            }
        }

        /// <summary>
        /// 创建一个长方体
        /// </summary>
        /// <param name="basePoint"></param>
        /// <param name="length"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <exception cref="ArgumentOutOfRangeException">当<paramref name="length"/>、<paramref name="width"/>、<paramref name="height"/> 不大于0 时抛出异常</exception>
        public Box(Point3D basePoint, double length, double width, double height)
        {
            if (length == 0 || width == 0 || height == 0) throw new ArgumentOutOfRangeException("尺寸不能为0！");

            BasePoint = basePoint;
            Length = length;
            Width = width;
            Height = height;
        }

        public GeometryInput ToGeometry()
        {
            if (Length == 0 || Width == 0 || Height == 0) throw new ArgumentOutOfRangeException("尺寸不能为0！");

            GeometryInput gi = new GeometryInput();
            Vector3D z = DirectorX.CrossProduct(DirectorY).Unit;

            // 底部四个点
            Point3D pt00 = BasePoint;
            Point3D pt01 = pt00 + DirectorX * Length;
            Point3D pt02 = pt01 + DirectorY * Width;
            Point3D pt03 = pt00 + DirectorY * Width;

            gi.AddVertex(pt00);
            gi.AddVertex(pt01);
            gi.AddVertex(pt02);
            gi.AddVertex(pt03);

            // 顶部四个点
            Point3D pt10 = BasePoint + z * Height;
            Point3D pt11 = pt10 + DirectorX * Length;
            Point3D pt12 = pt11 + DirectorY * Width;
            Point3D pt13 = pt10 + DirectorY * Width;

            gi.AddVertex(pt10);
            gi.AddVertex(pt11);
            gi.AddVertex(pt12);
            gi.AddVertex(pt13);

            // 添加六个面，注意顶点顺序，合适的顶点顺序才能使面的法向朝外
            gi.AddFace(0, 3, 2, 1);
            gi.AddFace(0, 1, 5, 4);
            gi.AddFace(0, 4, 7, 3);
            gi.AddFace(4, 5, 6, 7);
            gi.AddFace(1, 2, 6, 5);
            gi.AddFace(2, 3, 7, 6);

            return gi;
        }
    }
}